<?php
/**
 * 蜗牛博客
 * 本文件内代码属于“蜗牛博客”项目，禁止修改再发布本程序源码，禁止去除页面底部的版权申明。所有版权保留
 * User: upliu
 * Email: 753073861@qq.com
 * QQ: 753073861
 * 程序主页：http://demo.upliu.net/snail-blog/
 */

require __DIR__.'/init.php';

check_login();

switch(get('action')) {
    case 'save':
        if (post('new_password')) {
            foreach (array('old_password', 're_new_password') as $k) {
                if (empty($_POST[$k])) {
                    echo_json_error('表单（当前密码，新密码）请填写完整');
                }
            }
            if (!is_password_pass(get_current_login_user_info('salt'), get_current_login_user_info('password'), $_POST['old_password'])) {
                echo_json_error('当前密码不正确');
            }
            if (post('new_password') !== post('re_new_password')) {
                echo_json_error('两次输入密码不匹配');
            }
            $salt = generate_salt();
            $password = generate_password($salt, post('new_password'));
            $data = array(
                'salt' => $salt,
                'password' => $password
            );
        }
        $data['nickname'] = post('nickname');
        Db::getInstance()->update('user', $data, 'id=?', get_current_login_user_info('id'));
        if (post('new_password')) {
            logout();
            echo_json_success('密码设置成功，请重新登录', '', 'login.php');
        } else {
            echo_json_success('保存成功', '');
        }
        break;


    default:
        echo renderWithLayout('profile');
        break;
}
